/*******************************************************************************
* Long-term effects of weather-induced migration on urban labor and housing 
  markets
* Busso & Chauvin

* Purpose: Conduct Combes et al. first step (input for cities_panel_short.do)

*******************************************************************************/
	
/*******************************************************************************
Combes, Démurger, Li (2015) - First step - Labor market outcomes
*******************************************************************************/

/* 3.1. Residents and among recent migrants
---------------------------------------------------------------*/
foreach year in $cyrs_housing {
	local y = substr("`year'", 3, .)
	
	use "${db_tmp}/${geo}_pcity_ind_`y'", replace

	* Create database with fixed effects
	foreach if in "if migrant == 0" "if migrant_rur_urb2 == 1" { 
		
		preserve
		
		if "`if'" == "if migrant == 0" local cond = "r"	
		if "`if'" == "if migrant_rur_urb2 == 1" local cond = "g"
		
		cap drop all_vars
		gen all_vars = l_wage != . & age2_sh != . & age3_sh !=. & ///
		age4_sh != . & age5_sh != . & dpr != . & ms != . & hi != . & cold != .
		
		cap drop `cond'_city*
		sort ${geo}
		levelsof ${geo} `if' & all_vars == 1, local(cities_`cond')
		foreach n of local cities_`cond' {
			gen `cond'_city`n' = (${geo} == `n')
		}
		
		sleep 10000
		
		eststo clear 
		
		eststo: reg l_wage `cond'_city* $educ2 $age_tier ///
		`if' & all_vars == 1 [pw = wtper], noconstant 
		
		sleep 10000

		* Create a city-level database (for each variant) with estimated fixed effects 
		parmest, saving(${db_tmp}/combes_1st_a_l_wage_`cond'_`y'.dta, replace) 	
		sleep 10000
		use ${db_tmp}/combes_1st_a_l_wage_`cond'_`y'.dta, replace
		keep if strpos(parm, "city") > 0
		rename estimate cityfe_l_wage_`cond'
		gen ${geo} = substr(parm, -7, .)
		destring ${geo}, replace
		keep ${geo} cityfe_l_wage_`cond'
		save ${db_tmp}/combes_1st_a_l_wage_`cond'_`y', replace
		
		restore
	
	}
}
	
/* 3.2. By industry
---------------------------------------------------------------*/
foreach year in $cyrs_housing {
	local y = substr("`year'", 3, .)
	
	use "${db_tmp}/${geo}_pcity_ind_`y'", replace

	* Create database with fixed effects
	foreach if in "if migrant == 0" { 
		
		preserve
		
		if "`if'" == "if migrant == 0" local cond = "r"	
		
		cap drop all_vars
		gen all_vars = l_wage != . & age2_sh != . & age3_sh !=. & ///
		age4_sh != . & age5_sh != . & dpr != . & ms != . & hi != . & cold != . & ///
		isv_sh != . & imf_sh != .
		
		cap drop `cond'_city*
		sort ${geo}
		levelsof ${geo} `if' & all_vars == 1, local(cities_`cond')
		foreach n of local cities_`cond' {
			gen `cond'_city`n' = (${geo} == `n')
		}
		
		sleep 10000
		
		eststo clear 
		
		eststo: reg l_wage c.r_city*#c.isv_sh c.r_city*#c.imf_sh $educ2 $age_tier ///
		`if' & all_vars == 1 [pw = wtper], noconstant 
		
		sleep 10000

		* Create a city-level database (for each variant) with estimated fixed effects 
		parmest, saving(${db_tmp}/combes_1st_a_l_wage_`cond'_ind_`y'.dta, replace) 	
		sleep 10000
		use ${db_tmp}/combes_1st_a_l_wage_`cond'_ind_`y'.dta, replace
		
		keep if strpos(parm, "city") > 0
		gen ${geo} = substr(parm, 9, 7)
		gen isv = strpos(parm, "isv") > 0
		keep isv estimate ${geo}
		reshape wide estimate, i(${geo}) j(isv)
		rename estimate1 cityfe_l_wage_r_isv
		rename estimate0 cityfe_l_wage_r_imf
		destring ${geo}, replace

		save ${db_tmp}/combes_1st_a_l_wage_`cond'_ind_`y', replace
		
		restore
	
	}
}

/* 3.3. By schooling attainment
---------------------------------------------------------------*/
foreach year in $cyrs_housing {
	local y = substr("`year'", 3, .)
	
	use "${db_tmp}/${geo}_pcity_ind_`y'", replace

	* Create database with fixed effects
	foreach if in "if migrant == 0" { 
		
		preserve
		
		if "`if'" == "if migrant == 0" local cond = "r"	
		
		cap drop all_vars
		gen all_vars = l_wage != . & age2_sh != . & age3_sh !=. & ///
		age4_sh != . & age5_sh != . & dpr != . & ms != . & hi != . & cold != . & ///
		nohs != . & hs != .
		
		cap drop `cond'_city*
		sort ${geo}
		levelsof ${geo} `if' & all_vars == 1, local(cities_`cond')
		foreach n of local cities_`cond' {
			gen `cond'_city`n' = (${geo} == `n')
		}
		
		sleep 10000
		
		eststo clear 
		
		eststo: reg l_wage c.r_city*#c.nohs c.r_city*#c.hs $educ2 $age_tier ///
		`if' & all_vars == 1 [pw = wtper], noconstant 
		
		sleep 10000

		* Create a city-level database (for each variant) with estimated fixed effects 
		parmest, saving(${db_tmp}/combes_1st_a_l_wage_`cond'_hs_`y'.dta, replace) 	
		sleep 10000
		use ${db_tmp}/combes_1st_a_l_wage_`cond'_hs_`y'.dta, replace
		
		keep if strpos(parm, "city") > 0
		gen ${geo} = substr(parm, 9, 7)
		gen nohs = strpos(parm, "nohs") > 0
		keep nohs estimate ${geo}
		reshape wide estimate, i(${geo}) j(nohs)
		rename estimate1 cityfe_l_wage_r_nohs
		rename estimate0 cityfe_l_wage_r_hs
		
		gen aux_drop = strpos(${geo}, "y") > 0
		drop if aux_drop == 1
		drop aux_drop
		destring ${geo}, replace

		save ${db_tmp}/combes_1st_a_l_wage_`cond'_hs_`y', replace
		
		restore
	
	}
}


/*******************************************************************************
4. Combes, Démurger, Li (2015) - First step - Housing market outcomes
*******************************************************************************/

/* 4.1. General among residents, and among recent migrants
---------------------------------------------------------------*/
foreach year in $cyrs_housing {
	local y = substr("`year'", 3, .)
	
	use "${db_tmp}/${geo}_phous_ind_`y'", replace

	* Create database with fixed effects
	foreach if in "if pid == 1 & migrant == 0" "if pid == 1 & migrant_rur_urb2 == 1" { 
		
		preserve
		
		if "`if'" == "if pid == 1 & migrant == 0" local cond = "r"	
		if "`if'" == "if pid == 1 & migrant_rur_urb2 == 1" local cond = "g"	
		
		cap drop all_vars
		gen all_vars = l_rent != . & l_rooms != . & sh_sewer_access != . & ///
		trash != . 
		
		cap drop `cond'_city*
		sort ${geo}
		levelsof ${geo} `if' & all_vars == 1, local(cities_`cond')
		foreach n of local cities_`cond' {
			gen `cond'_city`n' = (${geo} == `n')
		}
		
		sleep 10000
		
		eststo clear 
		
		eststo: reg l_rent `cond'_city* $controls_housing ///
		`if' & all_vars == 1 [pw = wtper], noconstant 
		
		sleep 10000

		* Create a city-level database (for each variant) with estimated fixed effects 
		parmest, saving(${db_tmp}/combes_1st_a_l_rent_`cond'_`y'.dta, replace) 	
		sleep 10000
		use ${db_tmp}/combes_1st_a_l_rent_`cond'_`y'.dta, replace
		keep if strpos(parm, "city") > 0
		rename estimate cityfe_l_rent_`cond'
		gen ${geo} = substr(parm, -7, .)
		destring ${geo}, replace
		keep ${geo} cityfe_l_rent_`cond'
		save ${db_tmp}/combes_1st_a_l_rent_`cond'_`y', replace
		
		restore
	
	}
}

/* 4.2. By housing quality
---------------------------------------------------------------*/
foreach year in $cyrs_housing {
	local y = substr("`year'", 3, .)
	
	use "${db_tmp}/${geo}_phous_ind_`y'", replace
	
	* Create database with fixed effects
	foreach if in "if pid == 1 & migrant == 0" { 
		
		preserve
		
		if "`if'" == "if pid == 1 & migrant == 0" local cond = "rr"	
		
		cap drop all_vars
		gen all_vars = l_rent != . & l_rooms != . & sh_sewer_access != . & ///
		trash != . & hhquality_l != . & hhquality_m != . & hhquality_h !=.
		
		cap drop `cond'_city*
		sort ${geo}
		levelsof ${geo} `if' & all_vars == 1, local(cities_`cond')
		foreach n of local cities_`cond' {
			gen `cond'_city`n' = (${geo} == `n')
		}
		
		sleep 10000
		
		eststo clear 
		
		eststo: reg l_rent c.`cond'_city*#c.hhquality_l c.`cond'_city*#c.hhquality_m ///
		c.`cond'_city*#c.hhquality_h $controls_housing ///
		`if' & all_vars == 1 [pw = wtper], noconstant 
		
		sleep 10000

		* Create a city-level database (for each variant) with estimated fixed effects 
		parmest, saving(${db_tmp}/combes_1st_a_l_rent_`cond'_qual_`y'.dta, replace) 	
		sleep 10000
		use ${db_tmp}/combes_1st_a_l_rent_`cond'_qual_`y'.dta, replace
		keep if strpos(parm, "city") > 0
		gen ${geo} = substr(parm, 10, 7)
		gen low = strpos(parm, "quality_l") > 0
		gen med = strpos(parm, "quality_m") > 0
		gen high = strpos(parm, "quality_h") > 0
		gen quality = .
			replace quality = 1 if low == 1
			replace quality = 2 if med == 1
			replace quality = 3 if high == 1
		keep quality estimate ${geo}
		reshape wide estimate, i(${geo}) j(quality)
		rename estimate1 cityfe_l_rent_l_`cond'
		rename estimate2 cityfe_l_rent_m_`cond'
		rename estimate3 cityfe_l_rent_h_`cond'
		
		gen aux_drop = strpos(${geo}, "y") > 0
		drop if aux_drop == 1
		drop aux_drop
		destring ${geo}, replace
		
		save ${db_tmp}/combes_1st_a_l_rent_`cond'_qual_`y', replace
		
		restore
	
	}
}

